package com.bwie.goods.service.impl;

import com.bwie.common.domain.Collect;
import com.bwie.common.domain.PowerStation;
import com.bwie.common.result.Result;
import com.bwie.goods.mapper.CollectMapper;
import com.bwie.goods.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceimpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;

    @Override
    public Result<List<Collect>> list() {
        List<Collect> list=collectMapper.list();
        return Result.success(list);
    }

    @Override
    public Result collectAdd(Integer id) {
        //通过前台穿过来的id查询充电站信息
        PowerStation powerStation =collectMapper.powerStationAdd(id);

        //查询收藏表中是否有充电站信息
        Integer i = collectMapper.findById(id);

        if(i!=null){
            return Result.error("该充电站已被收藏,请勿重复收藏");
        }

        if (powerStation!=null){
            //将充电站信息添加到收藏表中
            Collect collect = new Collect();

            collect.setCollectId(id);
            collect.setCollectImg(powerStation.getPowerStationImg());
            collect.setCollectName(powerStation.getPowerStationName());
            collect.setCollectAddress(powerStation.getPowerStationAddress());
            collect.setCollectType(powerStation.getPowerStationType());
            collectMapper.collectAdd(collect);
        }
        return Result.success(powerStation,"收藏成功");
    }

    @Override
    public Result delete(Integer id) {
        Integer i=collectMapper.handleDelete(id);
        return Result.success(i>0?200:500,i>0?"取消收藏成功":"取消收藏失败");
    }
}
